package com.example.jdk12;

import java.text.NumberFormat;
import java.util.Locale;

public class CompactNumberFormattingTest {
    public static void main(String[] args) {
        NumberFormat compactFormat = NumberFormat.getCompactNumberInstance(Locale.CHINA, NumberFormat.Style.SHORT);
        System.out.println(compactFormat.format(1000));
        System.out.println(compactFormat.format(1500000));
        System.out.println(compactFormat.format(1200000000));

        System.out.println(format(1000));
        System.out.println(format(100000));
        System.out.println(format(1000000));
        System.out.println(format(900000000));
    }

    private static String format(long number) {
        if (number < 1000) {
            return Long.toString(number);
        } else if (number < 1_000_000) {
            double thousands = number / 1000.0;
            return String.format("%.1fk", thousands);
        } else if (number < 1_000_000_000) {
            double millions = number / 1_000_000.0;
            return String.format("%.1fM", millions);
        } else {
            return "Too large";
        }
    }
}
